<?php

namespace app\api\model\kuerp;

use think\Model;
use think\Db;
use think\Exception;
class Systems extends Model
{

    // 表名
    protected $name = 'kuerp_user_config';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    /**
     * 获取详情
     * @param int $id
     */
    public static function getDetail($name){
        $detail = self::where(function ($query) use ($name) {
            $query->where('name', 'in', $name);
        })->field('id,name,value')->select();
        return $detail;
    }

    /**
     * 添加
     * @param array $detail
     * @return array
     */
    public static function addDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[
                'name|设置项'=>'require',
                'value|内容'=>'require',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $model = new \app\api\model\kuerp\Systems();
                $row = $model->where(['name'=>$detail['name']])->find();
                if(!empty($row)){
                    return ['status'=>500,'msg'=>'设置项已存在'];
                }
                Db::startTrans();
                try {
                    $res = (new Systems())->allowField(true)->save($detail);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    return ['status'=>500,'msg'=>$e->getMessage()];
                }
                if($res){
                    return ['status'=>200,'msg'=>$res];
                }else{
                    return ['status'=>500,'msg'=>'添加失败'];
                }
            }
        }
    }

    /**
     * 更新
     * @param array $detail
     * @return array
     */
    public static function editDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[
                'id|ID'=>'require|number',
                'name|设置项'=>'require',
                'value|内容'=>'require',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $model = new \app\api\model\kuerp\Systems();
                $row = $model->where(['name'=>$detail['name'],'id'=>['<>',$detail['id']]])->find();
                if(!empty($row)){
                    return ['status'=>500,'msg'=>'设置项已存在'];
                }
                $row = $model->where(['id'=>$detail['id']])->find();
                if(empty($row)){
                    return ['status'=>500,'msg'=>'数据不存在'];
                }
                $detail['updatetime'] = time();
                Db::startTrans();
                try {
                    $res = $row->allowField(true)->save($detail);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    return ['status'=>500,'msg'=>$e->getMessage()];
                }
                if($res){
                    return ['status'=>200,'msg'=>'更新成功'];
                }else{
                    return ['status'=>500,'msg'=>'更新失败'];
                }
            }
        }
    }

}
